package main

import (
	"fmt"
	"os"
	"strings"
	"code.google.com/p/go.image/tiff"
	"alex/sensorager/histogram"
	"alex/sensorager/ager"
	"alex/msc/helper"
)

var (
	makeHisto bool = true
	doAging bool   = true
	histData []int = make([]int, 256, 256)
	path string    = "/Users/AlexZ/databases/"
	percent int    = 1
)

func main() {
	if makeHisto {
		openImages(path+"DB1_B/", "", false, false, uint8(0))
		openImages(path+"DB2_B/", "", false, false, uint8(0))
		histogram.MergeValues(histData)
		fmt.Println(histData)
	}
	if doAging {

		for percent < 27 {

			subpath := ""

			if percent < 10 {
				subpath = fmt.Sprintf("0%v", percent)
			} else {
				subpath = fmt.Sprintf("%v", percent)
			}

			for i := 1; i < 6; i++ {
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+fmt.Sprintf("_%v/", i),
					true, false, uint8(0))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+fmt.Sprintf("_%v/", i),
					true, false, uint8(0))
				fmt.Println("Histrogram done for ", i)
			}
			fmt.Println("Hsto done...")

			for i := 1; i < 4; i++ {
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_50_"+fmt.Sprintf("%v/", i),
					true, true, uint8(50))
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_100_"+fmt.Sprintf("%v/", i),
					true, true, uint8(100))
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_150_"+fmt.Sprintf("%v/", i),
					true, true, uint8(150))
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_200_"+fmt.Sprintf("%v/", i),
					true, true, uint8(200))
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_250_"+fmt.Sprintf("%v/", i),
					true, true, uint8(250))
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_B"+fmt.Sprintf("%v/", i),
					true, true, uint8(0))
				openImages(path+"DB1_B/", path+subpath+"/DB1_BA"+subpath+"_W"+fmt.Sprintf("%v/", i),
					true, true, uint8(255))
				fmt.Println("DB1 done...")

				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_50_"+fmt.Sprintf("%v/", i),
					true, true, uint8(50))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_100_"+fmt.Sprintf("%v/", i),
					true, true, uint8(100))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_150_"+fmt.Sprintf("%v/", i),
					true, true, uint8(150))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_200_"+fmt.Sprintf("%v/", i),
					true, true, uint8(200))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_250_"+fmt.Sprintf("%v/", i),
					true, true, uint8(250))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_B"+fmt.Sprintf("%v/", i),
					true, true, uint8(0))
				openImages(path+"DB2_B/", path+subpath+"/DB2_BA"+subpath+"_W"+fmt.Sprintf("%v/", i),
					true, true, uint8(255))
				fmt.Println("DB2 done....")
			}

			if percent < 6 {
				percent++
			} else {
				percent += 2
			}
			fmt.Printf("%v Percent done", percent)
		}
	}
}

func openImages(pathToImages string, pathToSave string, aging bool, set bool, col uint8) {
	//Opening directory
	directory, err := os.Open(pathToImages)
	helper.ErrHandling(err)
	defer directory.Close()

	//Scanning all directory files
	files, err := directory.Readdir(0)
	helper.ErrHandling(err)

	//Iterating over those files
	for _, file := range files {
		//Filtering Process - only *.tif files are processed
		if file.Mode().IsRegular() && strings.HasSuffix(file.Name(), ".tif") {
			name := pathToImages + file.Name()
			fi, err := os.Open(name)
			helper.ErrHandling(err)
			img, err := tiff.Decode(fi)
			helper.ErrHandling(err)

			if aging {
				ager.Aging(img, histData, percent, pathToSave+file.Name(), set, col)
			} else {
				histogram.ScanImages(img, histData)
			}
			fi.Close()
		}
	}
}

